package com.hnou.boxuegu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.utils.UtilsHelper;
import com.hnou.boxuegu.view.ExercisesView;
import com.hnou.boxuegu.view.MyInfoView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyInfoView mMyInfoView;
    protected FrameLayout mBodyLayout;
    public LinearLayout mBottomLayout;
    private View mCourseBth,mExercisesBth,mMyInfoBtn;
    private TextView tv_course,tv_exercises,tv_myInfo;
    private ImageView iv_course,iv_exercises,iv_myInfo;
    private TextView tv_back,tv_maim_title;
    private RelativeLayout rl_title_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setListener();
        selectDisplayView(0);
    }
    private  void init(){
        tv_back=findViewById(R.id.tv_back);
        tv_maim_title=findViewById(R.id.tv_main_title);
        tv_maim_title.setText("博学谷课程");
        rl_title_bar=findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_back.setVisibility(View.GONE);
        mBodyLayout=findViewById(R.id.main_body);
        mBottomLayout=findViewById(R.id.main_bottom_bar);
        mCourseBth=findViewById(R.id.bottom_bar_course_btn);
        mExercisesBth=findViewById(R.id.bottom_bar_exercises_btn);
        mMyInfoBtn=findViewById(R.id.bottom_bar_myinfo_btn);
        tv_course=findViewById(R.id.bottom_bar_text_course);
        tv_exercises=findViewById(R.id.bottom_bar_text_exercises);
        tv_myInfo=findViewById(R.id.bottom_bar_text_myinfo);
        iv_course=findViewById(R.id.bottom_bar_image_course);
        iv_exercises=findViewById(R.id.bottom_bar_image_exercises);
        iv_myInfo=findViewById(R.id.bottom_bar_image_myinfo);
    }
    private void setNotSelectedStatus(){
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercises.setTextColor(Color.parseColor("#666666"));
        tv_myInfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon);
        iv_myInfo.setImageResource(R.drawable.main_my_icon);
        for (int i =0;i<mBottomLayout.getChildCount();i++){
            mBottomLayout.getChildAt(i).setSelected(false);
        }
    }
    private void setSelectedStatus(int index){
        switch (index){
            case 0:
                mCourseBth.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                tv_course.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_maim_title.setText("博学谷课程");
                break;
            case 1:
                mExercisesBth.setSelected(true);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                tv_exercises.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_maim_title.setText("博学谷习题");
                break;
            case 2:
                mMyInfoBtn.setSelected(true);
                iv_myInfo.setImageResource(R.drawable.main_my_icon_selected);
                tv_myInfo.setTextColor(Color.parseColor("#0097F7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                break;
        }
    }
    private void hideAllView(){
        for (int i=0;i<mBodyLayout.getChildCount();i++){
            mBodyLayout.getChildAt(i).setVisibility(View.GONE);
        }
    }
    private void createView (int viewIndex){
        switch (viewIndex){
            case 0:
                break;
            case 1:
                if(mExercisesView==null){
                    mExercisesView =new ExercisesView(this);
                    mBodyLayout.addView(mExercisesView.getView());
                }else {
                    mExercisesView.getView();
                }
                mExercisesView.showView();
                break;
            case 2:
                if (mMyInfoView==null){
                    mMyInfoView=new MyInfoView(this);
                    mBodyLayout.addView(mMyInfoView.getView());
                }else {
                    mMyInfoView.getView();
                }
                mMyInfoView.showView();
                break;
        }
    }
    private void selectDisplayView(int index){
        hideAllView();
        createView(index);
        setSelectedStatus(index);
    }

    private void setListener(){
        for (int i=0;i<mBottomLayout.getChildCount();i++){
            mBottomLayout.getChildAt(i).setOnClickListener(this);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bottom_bar_course_btn:
                setNotSelectedStatus();
                selectDisplayView(0);
                break;
            case R.id.bottom_bar_exercises_btn:
                setNotSelectedStatus();
                selectDisplayView(1);
                break;
            case R.id.bottom_bar_myinfo_btn:
                setNotSelectedStatus();
                selectDisplayView(2);
                break;
        }
    }
    protected long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis()-exitTime)>2000){
                Toast.makeText(MainActivity.this, "在按一次退出博学谷", Toast.LENGTH_SHORT).show();
                exitTime=System.currentTimeMillis();
            }else {
                MainActivity.this.finish();
                if (UtilsHelper.readLoginStatus(MainActivity.this)){
                    UtilsHelper.clearLoginStatus(MainActivity.this);
                }
                System.exit(0);
            }
            return  true;
        }
        return super.onKeyDown(keyCode,event);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data!=null){
            boolean isLogin=data.getBooleanExtra("isLogin",false);
            if (isLogin){
                setNotSelectedStatus();
                selectDisplayView(0);
            }
            if (mMyInfoView!=null){
                mMyInfoView.setLoginParams(isLogin);
            }
        }
    }
}